import api from "@/utils/request";

export function getSystemFileUrl(path: string, fileName: string) {
    return api.get('/documentManager/fileBrowser/files/getLocalFileUrl?path=' + path + '&&fileName=' + fileName)
}

export function getFilesByPath(path: string) {
    return api.get('/documentManager/fileBrowser/files/getAllfileByFolder?path=' + path)
}

export function getFileAsText(fileName: any) {
    return api.get('/documentManager/fileBrowser/files/getText?fileName=' + fileName)
}

export function moveSystemFile(data: any) {
    return api.post('/documentManager/fileBrowser/files/moveFile', data)
}

export function newSystemFolder(currentPath: string, folderName: any) {
    return api.post('/documentManager/fileBrowser/files/newFolder?currentPath=' + currentPath + '&&folderName=' + folderName)
}

export function newSystemFile(currentPath: string, fileName: any) {
    return api.post('/documentManager/fileBrowser/files/newFile?currentPath=' + currentPath + '&&fileName=' + fileName)
}

export function uploadSystemFile(path: string, formData: any, config: any, fileName: string | null) {
    console.log("本地上传文件", "path: " + path + " fileName: " + fileName)
    return api.post('/documentManager/fileBrowser/files/upload?path=' + path + '&fileName=' + fileName, formData, config)
}

export function deleteSystemFile(path: string, type: number) {
    console.log(type)
    if (type === 0) {
        return api.post('/documentManager/fileBrowser/files/deleteFile?filePath=' + path)
    } else if (type === 1) {
        return api.post('/documentManager/fileBrowser/files/deleteFolder?path=' + path)
    }

}

// 重命名文件名
export function renameSystemFile(path: any, newName: any) {
    console.log("重命名文件名")
    console.log(path, newName)
    return api.post('/documentManager/fileBrowser/files/rename-file?path=' + path + '&newName=' + newName)
}

// 重命名文件夹名
export function renameSystemFolder(path: any, newPath: any) {
    console.log("重命名文件")
    console.log(path, newPath)
    return api.post('/documentManager/fileBrowser/files/rename-directory?path=' + path + '&newName=' + newPath)
}

export async function downloadSystemFile(fileName: string) {
    try {
        api.get('/documentManager/fileBrowser/files/download?filePath=' + fileName, {responseType: 'blob'})
            .then((res: any) => {
                const url = URL.createObjectURL(new Blob([res.data]));
                console.log("url", url)
                const link = document.createElement('a');
                console.log("link", link)
                link.href = url;
                link.setAttribute('download', fileName); // 或者你想要设置的下载文件名
                document.body.appendChild(link);
                link.click();
                link.remove();
            })

    } catch (error) {
        console.error("There was an error downloading the file!", error);
    }
}
